package com.wanxi.servlet;

import com.wanxi.dto.CompanyDto;
import com.wanxi.dto.ProductDto;
import com.wanxi.model.CompanyModel;
import com.wanxi.model.ProductModel;
import com.wanxi.service.CompanyService;
import com.wanxi.service.ProductService;
import com.wanxi.service.impl.CompanyServiceImpl;
import com.wanxi.service.impl.ProductServiceImpl;
import com.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/home")
public class HomeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        1。乱码
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
//        2.得到前端的值
        String name = req.getParameter("name");
        String imgHref = req.getParameter("imgHref");
        String content = req.getParameter("content");
        String normalPrice = req.getParameter("normalPrice");
        String marketPrice = req.getParameter("marketPrice");
        String cuisineId = req.getParameter("cuisineId");
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String enable = req.getParameter("enable");
//        3.封装到model
        CompanyModel companyModel = CompanyModel.builder()
                .build();

        ProductModel productModel = ProductModel.builder()
                .imgHref(imgHref)
                .marketPrice(Tool.strToDecimal(marketPrice))
                .normalPrice(Tool.strToDecimal(normalPrice))
                .content(content)
                .cuisineId(Tool.strToInt(cuisineId))
                .enable(Tool.strToInt(enable))
                .name(name)
                .page(Tool.strToPage(page))
                .limit(Tool.strToLimit(limit))
                .build();
//        4.调用服务逻辑层
        CompanyService companyService = new CompanyServiceImpl();
        CompanyDto companyDto = companyService.getCompanyDto(companyModel);

        ProductService productService = new ProductServiceImpl();
        List<ProductDto> list = productService.findAllFront(productModel);
        req.setAttribute("companyDto", companyDto);
        req.setAttribute("list", list);
//        Object companyDto1 = req.getAttribute("companyDto");
//        if(companyDto1==null){
//
//        }else {
//
//        }
//        5.返回页面
        req.getRequestDispatcher("/jsp/home.jsp").forward(req, resp);
//        resp.sendRedirect("/home.jsp");
    }
}
